local skill = fk.CreateSkill{
  name = "tea__guye",
}
---@type TrigSkelSpec<EventTurnChangingFunc>
local guye = {
  anim_type = "drawcard",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(skill.name) then
      local room = player.room
      local list = {}
      room.logic:getEventsOfScope(GameEvent.MoveCards, 1, function (e)
        for _, move in ipairs(e.data) do
          if move.from and move.moveReason ~= fk.ReasonUse and move.moveReason ~= fk.ReasonResponse then
            table.insert(list, {move.from.id, #move.moveInfo})
          end
        end
        return false
      end, Player.HistoryPhase)
      local targets = table.filter(room.alive_players, function(p)
        local counts = 0
        for _, v in ipairs(list) do
          if v[1] == p.id then
            counts = counts + v[2]
          end
        end
        return counts > 1
      end)
      if #targets > 0 then
        event:setCostData(self, targets)
        return true
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local targets = event:getCostData(self)
    local tos = room:askToChoosePlayers(player, {
      targets = targets,
      min_num = 1,
      max_num = 999,
      prompt = "#tea__guye-invoke",
      skill_name = skill.name
    })
    if #tos > 0 then
      event:setCostData(self, tos)
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local targets = event:getCostData(self)
    room:sortByAction(targets)
    for _, p in ipairs(targets) do
      if not p.dead then
        p:drawCards(1, skill.name)
      end
    end
  end,
}
skill:addEffect(fk.EventPhaseEnd, guye)
return skill